﻿using System;
using System.Data;
using System.IO;
using ExcelDataReader;

namespace SimpleEditor.ExcelTool
{
    public class ExcelReaderHelper
    {
        public static DataSet Read(string excelPath)
        {
            var extension = Path.GetExtension(excelPath);
            FileStream fileStream = new FileStream(excelPath, FileMode.Open);
            IExcelDataReader excelDataReader = null;
            if (extension == ".xls")
            {
                excelDataReader = ExcelReaderFactory.CreateBinaryReader(fileStream);
            }
            else if (extension == ".xlsx")
            {
                excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
            }

            if (excelDataReader != null)
            {
                var dataSet = excelDataReader.AsDataSet();
                
                fileStream.Close();
                excelDataReader.Close();
                return dataSet;
            }

            throw new Exception("Excel File ERROR !!!");
        }
    }
}